import Server from "@/config/server";
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode : false,
  dataLists: [],
  Fields: [],
  itemDetail: {},
  columnData: [],
  payRecordFields: [],
  payRecordDataLists: {},
};
const actions = {
  //列表
  getLists({commit}, page) {
    request.get(Server.action.settlement_list, { params: page[0] })
    .then(response => {
      console.log(response);
        if (response.data.errorCode === 0) {
            commit("setLists", response.data.result.list);
            let fields = response.data.result.fields;
            if(page[1]){
              fields.push({
                  title: '操作',
                  align: 'center',
                  width: 70,
                  scopedSlots: { customRender: 'action' },
                  fixed: 'right',
              })
            }
            fields[0].width = 40
            fields[0].fixed = 'left'
            fields.forEach(function (item, i) {
              fields[i].dataIndex = fields[i].key
              fields[i].align = 'center';
              if(page[1]){
                if(i < fields.length-2) {
                  fields[i].width = item.width || 100
                }
              } else {
                if(i < fields.length-1) {
                  fields[i].width = item.width || 100
                }
              }
              
            })
            commit("setFields", fields);
        } else {

        }
    })
  },
  
  //更改结算状态
  setTlementStatus({commit}, fromData) {
    commit("setErrorCode", false);
    request.get(Server.action.settlement_Status, { params: fromData } )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  //支付记录列表
  getPayRecordLists({commit}, page) {
    request.get(Server.action.trade_list, { params: page })
    .then(response => {
      console.log(response);
        if (response.data.errorCode === 0) {
            commit("setpayRecordLists", response.data.result.list);
            let fields = response.data.result.fields;
            fields[0].width = 60
            fields.forEach((item, i)=> {
              item.dataIndex = item.key
              item.align = 'center';
              item.width = item.width || 90
            })
            fields.push({
              title: '操作',
              align: 'center',
              width: 70,
              scopedSlots: { customRender: 'action' },
              // fixed: 'right',
            })
            commit("setpayRecordFields", fields);
        } else {

        }
    })
  },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
	},
	setLists(state, lists) {
	    state.dataLists = lists;
	},
  setFields(state, lists) {
	    state.Fields = lists;
	},
  setDetail(state, data) {
      state.itemDetail = data;
  },
  setColumnAll(state, data) {
    state.columnData = data;
  },
  setpayRecordLists(state, lists) {
    state.payRecordDataLists = lists;
  },
  setpayRecordFields(state, lists) {
      state.payRecordFields = lists;
  },
};
export default {
    namespaced: true,
    state,
    actions,
    mutations
};
